package com.demo.apparel.controller.web;

import com.alibaba.excel.EasyExcel;
import com.demo.apparel.common.Result;
import com.demo.apparel.dto.web.UserBatchDeleteDTO;
import com.demo.apparel.entity.WxUser;
import com.demo.apparel.service.web.WebWxUserService;
import com.demo.apparel.vo.PageVO;
import com.demo.apparel.vo.web.WebWxUserPageRequestVO;
import com.demo.apparel.vo.web.WebWxUserVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

@RestController
@RequestMapping("/web/user")
@Tag(name = "微信用户接口", description = "微信用户信息")
public class WebWxUserController {

    @Autowired
    private WebWxUserService webWxUserService;

    /**
     *
     * @param request
     * @return
     */
    @PostMapping("/list")
    @Operation(summary = "获取用户的信息")
    public Result<PageVO<WxUser>> getUsersPage(@RequestBody WebWxUserPageRequestVO request) {
        PageVO<WxUser> page = webWxUserService.getUsersPage(
                request.getPageNum(),
                request.getPageSize(),
                request.getUsername(),
                request.getPhone()
        );
        return Result.success(page);
    }

    /**
     *
     * @param response
     */
    @GetMapping("/export")
    @Operation(summary = "导出数据")
    public void exportUsers(HttpServletResponse response) {
        List<WebWxUserVO> users = webWxUserService.exportUsers();

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("用户数据", StandardCharsets.UTF_8);
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

        try {
            EasyExcel.write(response.getOutputStream(), WebWxUserVO.class)
                    .sheet("用户列表")
                    .doWrite(users);
        } catch (IOException e) {
            throw new RuntimeException("导出失败", e);
        }
    }


    /**
     * 删除用户
     */
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除用户")
    public Result<Void> deleteUser(@PathVariable String id) {
        boolean deleted = webWxUserService.deleteUserById(id);
        return deleted ? Result.success() : (Result<Void>) Result.fail("删除失败");
    }



    @PostMapping("/deleteBatch")
    @Operation(summary = "批量删除用户")
    public Result<Void> deleteUsers(@RequestBody UserBatchDeleteDTO dto) {
        boolean success = webWxUserService.deleteUsersByIds(dto.getId());
        return success ? Result.success() : (Result<Void>) Result.fail("批量删除成功");
    }

    @GetMapping("/all")
    @Operation(summary = "获取所有微信用户")
    public Result<List<WxUser>> getAllUsers() {
        List<WxUser> userList = webWxUserService.getAllUsers();
        return Result.success(userList);
    }

}
